package com.itbour.web.action.tg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.NotifyInfoDto;
import com.itbour.service.tg.NotifyInfoService;
import com.opensymphony.xwork2.ActionContext;

public class GetNotifyInfoWebAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private NotifyInfoService notifyInfoService;
	private String findTitleName;//标题名称
	private String findDateStart;
	private String findDateEnd;
	private String findNotifyType;//根据分类来查询
	private NotifyInfoDto notifyInfo;// 更新用
	HashMap<String, Object> mapNotifyInfoResult = new HashMap<String, Object>();
	HashMap<String, Object> mapNotifyEditMsg = new HashMap<String, Object>();
	HashMap<String, Object> mapNotifyUserMsg = new HashMap<String, Object>();

	private List<NotifyInfoDto> notifyInfoList;// 专题信息列表
	private String id;// 标题ID
	private String ids;// 多个标题ID
	private String number;// 序号
	private String findMobile;// 手机号查询
	private String notify_id;//
	private String findValid;//	消息状态 10/未开始 20/进行中 30/已结束
	
	
	/**	列表获取信息
	 * 	参数：消息状态/findValid
	 */
	public String getNotifyInfoPg() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findTitleName)){
				param.put("title", findTitleName);
			}
			if (StringUtils.isNotBlank(findDateStart)){
				param.put("start_time", findDateStart);
			}
			if (StringUtils.isNotBlank(findDateEnd)){
				param.put("end_time", findDateEnd);
			}
			if (StringUtils.isNotBlank(findNotifyType)){
				param.put("type", findNotifyType);
			}
			if (StringUtils.isNotBlank(findValid)){// 消息状态 
				param.put("valid",findValid);
			}
			
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			// 翻页查询
			Page page = notifyInfoService.pageQueryStats(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			//writeJSON(resMap);
			mapNotifyInfoResult = resMap;
		} else {
			mapNotifyInfoResult.clear();
		}
		return SUCCESS;
	}
	public String getFindValid() {
		return findValid;
	}
	public void setFindValid(String findValid) {
		this.findValid = findValid;
	}



	// 获取专题信息列表
	public String getNotifyInfoLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("id", id);
		}
		notifyInfoList = notifyInfoService.selectNotifyInfo(param);
		return SUCCESS;
	}
	// 删除专题信息
	public void delNotify() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		notifyInfoService.delTopic(ids);
		this.writeJSON("ok");
	}
	
	// 新增消息信息
	public void addNotify() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
		if (notifyInfo != null && user != null) {
			notifyInfo.setUpdate_by(user.getUser_id().toString());
	    	
	    	if(notifyInfo.getId()!=null && notifyInfo.getId()>0){
	    		// 更新
	    		this.updNotify();
	    	} else {
	    		// 新建
	    		notifyInfoService.addNotify(notifyInfo);
	    		this.writeJSON("ok");
	    	}
		} else {
			this.writeJSON("ng");
		}
	}
	// 添加关联消息用户
	public void addNotifyMsg() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
		if (notifyInfo != null && user != null) {
			notifyInfo.setUpdate_by(user.getUser_id().toString());
	     	notifyInfoService.addNotifyMsg(notifyInfo);
	     	this.writeJSON("ok");
		}else{
			this.writeJSON("no");
		}
	}
	// 更新专题信息
	public void updNotify() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		if(user != null && notifyInfo != null && notifyInfo.getId()!=null) {
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("id", notifyInfo.getId());
	    	param.put("update_by", user.getUser_id());
	    	
	    	if(notifyInfo.getType()>0) {
	    		param.put("type", notifyInfo.getType());
	    	}
	    	if(notifyInfo.getEncrypt()!=null) {
	    		param.put("encrypt", notifyInfo.getEncrypt());
	    	}
	    	if(notifyInfo.getTitle()!= null) {
	    		param.put("title", notifyInfo.getTitle());//标题
	    	}
	    	if(notifyInfo.getMessage()!= null) {
	    		param.put("message", notifyInfo.getMessage());
	    	}
	    	if(notifyInfo.getImage()!= null) {
	    		param.put("image", notifyInfo.getImage());
	    	}
	      	if(notifyInfo.getUrl()!= null) {
	    		param.put("url", notifyInfo.getUrl());
	    	}
	       	if(notifyInfo.getStart_time()!= null) {
	    		param.put("start_time", notifyInfo.getStart_time());
	    	}
	       	if(notifyInfo.getEnd_time()!= null) {
	    		param.put("end_time", notifyInfo.getEnd_time());
	    	}
	       	if(notifyInfo.getChk_freq()!= null) {
	    		param.put("chk_freq", notifyInfo.getChk_freq());
	    	}
	       	if(notifyInfo.getDisplay_type()!= null) {
	       		param.put("display_type", notifyInfo.getDisplay_type());
	       	}
	       	if(notifyInfo.getEmbed_url()!= null) {
	       		param.put("embed_url", notifyInfo.getEmbed_url());
	       	}
	       	if(notifyInfo.getEmbedW()!= null) {
	       		param.put("w", notifyInfo.getEmbedW());
	       	}
	       	if(notifyInfo.getEmbedH()!= null) {
	       		param.put("h", notifyInfo.getEmbedH());
	       	}
	    	notifyInfoService.updNotify(param);
	    	this.writeJSON("ok");
	    	
		} else {
			this.writeJSON("ng");
		}
	}
	
	// 修改专题显示顺序
	public void updNotifyOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id=request.getParameter("orderid");
		String number=request.getParameter("number");
		if (StringUtils.isNotBlank(id)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	// update
	    	if(user!=null && user.getUser_id()!=null){
		    	Map<String, Object> param = new HashMap<String, Object>();
				param.put("number", Integer.valueOf(number));
				param.put("id", Long.valueOf(id));
				param.put("update_by", user.getUser_id());
				notifyInfoService.updNotify(param);
	    	}
		}
		this.writeJSON("ok");
	}
	
	public String  getNotifyEditMsgPg() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String, Object> param = new HashMap<String, Object>();//条件查询 
			if(StringUtils.isNotBlank(notify_id)){
				param.put("id", notify_id);
			}
		Page page = notifyInfoService.pageQueryNotifyEditMsg(param, Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		mapNotifyEditMsg = resMap;
		}else{
			mapNotifyEditMsg.clear();
		}
		return SUCCESS;
	}
	
	
	public String getNotifyUserMsgPg() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String, Object> param = new HashMap<String, Object>();//条件查询 
			if(StringUtils.isNotBlank(findMobile)){
				param.put("mobile", findMobile);
			}
			if(StringUtils.isNotBlank(notify_id)){
				param.put("id", notify_id);
			}
		Page page = notifyInfoService.pageQueryNotifyUserMsg(param, Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		mapNotifyUserMsg = resMap;
		}else{
			mapNotifyUserMsg.clear();
		}
		return SUCCESS;
	}
	
	public NotifyInfoService getNotifyInfoService() {
		return notifyInfoService;
	}
	public HashMap<String, Object> getMapNotifyInfoResult() {
		return mapNotifyInfoResult;
	}
	public void setNotifyInfoService(NotifyInfoService notifyInfoService) {
		this.notifyInfoService = notifyInfoService;
	}
	public void setMapNotifyInfoResult(HashMap<String, Object> mapNotifyInfoResult) {
		this.mapNotifyInfoResult = mapNotifyInfoResult;
	}
	public String getFindTitleName() {
		return findTitleName;
	}
	public void setFindTitleName(String findTitleName) {
		this.findTitleName = findTitleName;
	}
	public String getFindDateStart() {
		return findDateStart;
	}
	public String getFindDateEnd() {
		return findDateEnd;
	}
	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}

	public List<NotifyInfoDto> getNotifyInfoList() {
		return notifyInfoList;
	}

	public void setNotifyInfoList(List<NotifyInfoDto> notifyInfoList) {
		this.notifyInfoList = notifyInfoList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	
	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setNotifyInfo(NotifyInfoDto notifyInfo) {
		this.notifyInfo = notifyInfo;
	}
	
	public NotifyInfoDto getNotifyInfo() {
		return notifyInfo;
	}

	public String getFindNotifyType() {
		return findNotifyType;
	}

	public void setFindNotifyType(String findNotifyType) {
		this.findNotifyType = findNotifyType;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public HashMap<String, Object> getMapNotifyEditMsg() {
		return mapNotifyEditMsg;
	}

	public void setMapNotifyEditMsg(HashMap<String, Object> mapNotifyEditMsg) {
		this.mapNotifyEditMsg = mapNotifyEditMsg;
	}

	public HashMap<String, Object> getMapNotifyUserMsg() {
		return mapNotifyUserMsg;
	}

	public void setMapNotifyUserMsg(HashMap<String, Object> mapNotifyUserMsg) {
		this.mapNotifyUserMsg = mapNotifyUserMsg;
	}

	public String getFindMobile() {
		return findMobile;
	}

	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}

	public String getNotify_id() {
		return notify_id;
	}

	public void setNotify_id(String notify_id) {
		this.notify_id = notify_id;
	}

}
